﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class InventoryUerRepository : RepositoryBase<InventoryUer>
    {
        /// <summary>
        ///  根据产品id获取订单库存列表
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IQueryable<InventoryUserView> GetListByProduct(Guid pid)
        {
            var result = from invUser in dbcontext.InventoryUers
                         join user in dbcontext.Users on invUser.UserID equals user.Id into userEmp
                         from user in userEmp.DefaultIfEmpty()
                         join order in dbcontext.Orders on invUser.OrderID equals order.Id into orderEmp
                         from order in orderEmp.DefaultIfEmpty()
                         join seller in dbcontext.Users on order.UserId equals seller.Id into sellerEmp
                         from seller in sellerEmp.DefaultIfEmpty()
                         where invUser.ProductID == pid && invUser.Amount > 0
                         select new InventoryUserView
                         {
                             Id = invUser.Id,
                             ProductID = invUser.ProductID,
                             OrderID = invUser.OrderID,
                             UserID = invUser.UserID,
                             Amount = invUser.Amount,
                             UserName = user.RealName,
                             Number = order.Number,
                             CusNumber = order.CusNumber,
                             //SellerID=seller.Id,
                             SellerName = seller.RealName
                         };
            return result;
        }

        /// <summary>
        /// 获取订单库存详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public InventoryUserView GetInventoryUserByID(Guid id)
        {
            var result = from invUser in dbcontext.InventoryUers
                         join user in dbcontext.Users on invUser.UserID equals user.Id into userEmp
                         from user in userEmp.DefaultIfEmpty()
                         join order in dbcontext.Orders on invUser.OrderID equals order.Id into orderEmp
                         from order in orderEmp.DefaultIfEmpty()
                         join pro in dbcontext.Products on invUser.ProductID equals pro.Id into proEmp
                         from pro in proEmp.DefaultIfEmpty()
                         where invUser.Id == id
                         select new InventoryUserView
                         {
                             Id = invUser.Id,
                             ProductID = invUser.ProductID,
                             ProductName = pro.Name,
                             OrderID = invUser.OrderID,
                             UserID = invUser.UserID,
                             Amount = invUser.Amount,

                             UserName = user.RealName,
                             Number = order.Number,
                             CusNumber = order.CusNumber,
                         };
            return result.FirstOrDefault();
        }
    }
}
